using MediatR;
using Microsoft.AspNetCore.Mvc;
using WMS.Domain;
using WMS.ErrorCode;
using WMS.WebApiRead.Application.Command;

namespace WMS.WebApiRead.Controllers
{
    /// <summary>
    /// 仓库部门读操作控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseDeptController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<WarehouseDeptController> logger;

        /// <summary>
        /// 构造函数，注入MediatR和日志服务
        /// </summary>
        public WarehouseDeptController(IMediator mediator, ILogger<WarehouseDeptController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 获取部门级联数据（用于Element Plus Cascader组件）
        /// </summary>
        /// <param name="parentId">父部门ID，默认0表示从根部门开始</param>
        /// <returns>返回部门级联树结构</returns>
        [HttpGet]
        public async Task<ApiResult<List<CascaderInfo>>> GetDeptCascader([FromQuery] int parentId = 0)
        {
            logger.LogInformation($"开始查询部门级联数据，父部门ID：{parentId}");
            
            return await mediator.Send(new WarehousedeptsCascaderCommand
            {
                ParentId = parentId
            });
        }
    }
}

